home *** CD-ROM | disk | FTP | other *** search
/ Floppyshop 2 / Floppyshop - 2.zip / Floppyshop - 2.iso / diskmags / 0022-3.564 / dmg-0122 / st_appl.1_5 / p_forum3 / listing4.txt < prev   
Text File  |  1997-04-16  |  7KB  |  213 lines

  1. /*
  2. ** Listing 4. 
  3. **
  4. ** The new keyboard handling function
  5. ** with more useful text validation.
  6. */
  7.  
  8. /*
  9. ** Some keycodes:
  10. */
  11.  
  12. #define ESCAPE      0x011B
  13. #define BACKSPACE   0x0E08
  14. #define TAB         0x0F09
  15. #define RETURN      0x1C0D
  16. #define UP          0x4800
  17. #define SHFTUP      0x4838
  18. #define LEFT        0x4B00
  19. #define SHFTLF      0x4B34
  20. #define RIGHT       0x4D00
  21. #define SHFTRT      0x4D36
  22. #define DOWN        0x5000
  23. #define SHFTDN      0x5032
  24. #define DELETE      0x537F
  25. #define ENTER       0x720D
  26.  
  27. #define V_MAGIC     0x31415926
  28.  
  29. /*
  30. ** Function prototypes.
  31. */
  32.  
  33. int xform_keybd(OBJECT *,short,short,short *,short *,short *);
  34. short form_valid(char *,short,short *);
  35.  
  36. short  (* _validate)(short *, char *, short *) = NULL;
  37. int _valmagic = 0;
  38.  
  39.  
  40. /*
  41. ** Define the large table of character properties
  42. ** used to determine character validity. Note that
  43. ** 0 is not checked because it is never valid.
  44. ** The validation codes are specified as a bitmap in
  45. ** the following format: (bit set -> character acceptable)
  46. **   MSB  ..xpnaXTPNMHFBA9  LSB  (. unused bit)
  47. */
  48.  
  49. short _valtab[] = {
  50. 0x0200, 0x0200, 0x0200, 0x0200, 0x0200, 0x0200, 0x0200, 0x0200, /*  1- 8  */
  51. 0x0200, 0x0200, 0x0200, 0x0200, 0x0200, 0x0200, 0x0200, 0x0200, /*  9-16  */
  52. 0x0200, 0x0200, 0x0200, 0x0200, 0x0200, 0x0200, 0x0200, 0x0200, /* 17-24  */
  53. 0x0200, 0x0200, 0x0200, 0x0200, 0x0200, 0x0200, 0x0200, 0x2E66, /* 25-32  */
  54. 0x2200, 0x2200, 0x2200, 0x2200, 0x2200, 0x2200, 0x2200, 0x2200, /* ! to ( */
  55. 0x2200, 0x3208, 0x2200, 0x2200, 0x2200, 0x2200, 0x2200, 0x3BF9, /* ) to 0 */
  56. 0x3BF9, 0x3BF9, 0x3BF9, 0x3BF9, 0x3BF9, 0x3BF9, 0x3BF9, 0x3BF9, /* 1 to 8 */
  57. 0x3BF9, 0x3288, 0x2200, 0x2200, 0x2200, 0x2200, 0x3208, 0x2200, /* 9 to @ */
  58. 0x3FFE, 0x3FFE, 0x3FFE, 0x3FFE, 0x3FFE, 0x3FFE, 0x3FEE, 0x3FEE, /* A to H */
  59. 0x3FEE, 0x3FEE, 0x3FEE, 0x3FEE, 0x3FEE, 0x3FEE, 0x3FEE, 0x3FEE, /* I to P */
  60. 0x3FEE, 0x3FEE, 0x3FEE, 0x3FEE, 0x3FEE, 0x3FEE, 0x3FEE, 0x3FEE, /* Q to X */
  61. 0x3FEE, 0x3FEE, 0x2200, 0x3280, 0x2200, 0x2200, 0x1B88, 0x2200, /* Y to ` */
  62. 0x2E00, 0x2E00, 0x2E00, 0x2E00, 0x2E00, 0x2E00, 0x2E00, 0x2E00, /* a to h */
  63. 0x2E00, 0x2E00, 0x2E00, 0x2E00, 0x2E00, 0x2E00, 0x2E00, 0x2E00, /* i to p */
  64. 0x2E00, 0x2E00, 0x2E00, 0x2E00, 0x2E00, 0x2E00, 0x2E00, 0x2E00, /* q to x */
  65. 0x2E00, 0x2E00, 0x2200, 0x2200, 0x2200, 0x2200, 0x2200          /* to $7F */
  66.                   };
  67.  
  68.  
  69. /*
  70. ** Function to process the result of a keypress.
  71. ** The arguments are a pointer to the tree, the index
  72. ** of the current editable object, the keyboard shift
  73. ** state, a pointer to the index of the next object to
  74. ** go to after this, a pointer to the 2 unsigned char keycode of
  75. ** the key pressed and a pointer to the character index
  76. ** within the editable object.  The function may alter
  77. ** 'next' (for moving the edit cursor around), 'key'
  78. ** (to force certain key interpretations, 0 if no work
  79. ** to be done) and 'index' (for moving about in the line).
  80. ** The function returns FALSE if the edit is over, TRUE
  81. ** if not.
  82. **
  83. ** Usage:   result = xform_keybd(tree,edobj,shift,next,key,index);
  84. */
  85.  
  86. int xform_keybd(tree,edobj,shift,next,key,index)
  87.  
  88. register OBJECT *tree;
  89. short edobj;
  90. register short shift;
  91. register short *next, *key, *index;
  92.  
  93. {
  94.     short new_key;
  95.     char temp;
  96.     char *text;
  97.  
  98.     if (*key == TAB)
  99.         *key = (shift & 0x03 ? UP : DOWN);
  100.     if (edobj == -1 && *key != ENTER && *key != RETURN)
  101.         return (TRUE);
  102.     switch (*key)
  103.         {
  104.         case DELETE: case BACKSPACE: case ESCAPE:
  105.         case RIGHT:  case LEFT:      case UP:
  106.         case DOWN:   case ENTER:
  107.         case RETURN:    return(form_keybd(tree,0,edobj,*key,next,key));
  108.  
  109.         case SHFTRT:    objc_edit(tree,edobj,0,index,EDEND);
  110.                         objc_edit(tree,edobj,0,index,EDINIT);
  111.                         break;
  112.  
  113.         case SHFTLF:    objc_edit(tree,edobj,0,index,EDEND);
  114.                         *index = 0;
  115.                         objc_edit(tree,edobj,0,index,EDEND);
  116.                         break;
  117.  
  118.         default:        text = ((TEDINFO *)(tree+edobj)->ob_spec)->te_pvalid + *index;
  119.                         if (*text == '\0')
  120.                             text--;
  121.                         if (new_key = form_valid(text,*key,&edobj))
  122.                             {
  123.                             temp = *text;
  124.                             *text = 'X';
  125.                             objc_edit(tree,edobj,new_key,index,ED_CHAR);
  126.                             *text = temp;
  127.                             }
  128.                         else
  129.                             return(form_keybd(tree,0,edobj,*key,next,key));
  130.                         break;
  131.         }
  132.     *key = 0;
  133.     return (TRUE);
  134. }
  135.  
  136.  
  137. /*
  138. ** Function to determine whether the key pressed
  139. ** by the user is valid for this position in the
  140. ** field.  The function accepts a pointer to the
  141. ** current position in the validation string, the
  142. ** proposed character and a pointer to the index
  143. ** of the object being edited. It returns 0 if the
  144. ** character is to be ignored, or a character code
  145. ** (possibly modified) to be used.
  146. **
  147. ** Usage:   result = form_valid(text,keypress,obj);
  148. **
  149. **          char result, form_valid();
  150. **          char *text;
  151. **          short keypress;
  152. **          short *obj;
  153. */
  154.  
  155. short form_valid(text,keypress,obj)
  156.  
  157. register char *text;
  158. short keypress;
  159. short *obj;
  160.  
  161. {
  162.     register short mask;
  163.  
  164.     mask = 0;
  165.     keypress &= 0xFF;
  166.     if (keypress == '\0' || keypress > 127 && *text != 'X')
  167.         return (0);
  168.     switch (*text)
  169.         {
  170.         case '9':   mask = 0x0001;
  171.                     break;
  172.         case 'A':   mask = 0x0002;
  173.                     break;
  174.         case 'B':   mask = 0x0004;
  175.                     keypress = toupper(keypress);
  176.                     break;
  177.         case 'F':   mask = 0x0008;
  178.                     break;
  179.         case 'H':   mask = 0x0010;
  180.                     keypress = toupper(keypress);
  181.                     break;
  182.         case 'M':   mask = 0x0020;
  183.                     keypress = toupper(keypress);
  184.                     break;
  185.         case 'N':   mask = 0x0040;
  186.                     break;
  187.         case 'P':   mask = 0x0080;
  188.                     keypress = toupper(keypress);
  189.                     break;
  190.         case 'T':   mask = 0x0100;
  191.                     keypress = toupper(keypress);
  192.                     break;
  193.         case 'X':   mask = 0x0200;
  194.                     break;
  195.         case 'a':   mask = 0x0400;
  196.                     break;
  197.         case 'n':   mask = 0x0800;
  198.                     break;
  199.         case 'p':   mask = 0x1000;
  200.                     keypress = toupper(keypress);
  201.                     break;
  202.         case 'x':   mask = 0x2000;
  203.                     break;
  204.         default:    if (_valmagic == V_MAGIC && _validate != NULL)
  205.                         return ( (* _validate)(obj,text,&keypress));
  206.                     return (0);
  207.         }
  208.     if (_valtab[keypress-1] & mask)
  209.         return (keypress);
  210.     return (0);
  211. }
  212.  
  213.